mui.init({
	beforeback: function() {
		var list = plus.webview.currentWebview("../bookdetails/details.html").opener();
		mui.fire(list, 'refresh');
		return true;
	},
	pullRefresh: {
		container: '#offCanvasContentScroll',
		down: {
			callback: pulldownRefresh
		},
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});
var bookshelf = new Vue({
	el: '.main',
	data: {
		booksl: [],
		pingfen: [],
		trues: [],
		state: ''
	},
	updated: function() {}
});
if(localStorage.getItem("state") == "true") {
	bookshelf.state = true
} else {
	bookshelf.state = false
}
var kaiguan = false
$(document).on("tap", ".mainclass", function(e) {
	if(!$(e.target).is('button')) {
		common.open("../bookdetails/details.html", "../bookdetails/details.html", $(this).attr("bookuid"))
	}
})
$(document).on("tap", ".add", function(e) {
	if(kaiguan) {
		return
	}
	var self = $(this)
	kaiguan = true
	common.ajax("student/myShelf/addBook", {
		uid: common.uid,
		token: common.token,
		bookUid: $(this).parents(".mainclass").attr("bookuid")
	}, function(data) {
		if(data.code == 200) {
			mui.toast("添加成功")
			kaiguan = false
			self.addClass("added")
			self.text("已添加")
			var load = plus.webview.getWebviewById("bookshelf.html");
			mui.fire(load, "index");
			var go = plus.webview.getWebviewById("../stacks/Recommended.html")
			mui.fire(go, "refresh")
		} else {
			mui.toast(data.message)
			kaiguan = false
		}
	})
	e.stopPropagation()
})

var temp = 1
/**
 * 下拉刷新具体业务实现
 */
function pulldownRefresh() {
	setTimeout(function() {
		mui('#offCanvasContentScroll').pullRefresh().endPulldownToRefresh()
	}, 400)

}
/**
 * 上拉加载具体业务实现
 */
var seach = localStorage.getItem("seach")
console.log(seach)
//var seach = '一粒'

function pullupRefresh() {
	var self = this
	common.ajax('student/bookCity/queryAllBook', {
		uid: common.uid,
		token: common.token,
		keyWord: seach,
		page: temp++,
		pageSize: "8"
	}, function(data) {
		if(data.code == 200) {
			if(!data.data.hasNextPage) {
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh(true)
			} else {
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh()
			}
			datafor(data)
		} else {
			mui.toast(data.message)
		}
	}, 'get')
}
if(mui.os.plus) {
	mui.plusReady(function() {
		setTimeout(function() {
			mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
		}, 1000);

	});
} else {
	mui.ready(function() {
		mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
	});
}

function datafor(data) {
	for(var i = 0; i < data.data.list.length; i++) {
		if(String(data.data.list[i].mark).indexOf(".") != -1) {
			bookshelf.trues.push("scoring_two")
		} else {
			bookshelf.trues.push("")
		}
		bookshelf.booksl.push(data.data.list[i])
		bookshelf.pingfen.push(Math.floor(data.data.list[i].mark))
	}

	bookshelf.$nextTick(function() {
		var addbook = JSON.parse(localStorage.getItem('books')) || []
		//		console.log(JSON.stringify(addbook))
		$.each(addbook, function(e, a) {
			$.each($(".joinissue"), function(index, txt) {
				if($(this).parents(".mainclass").attr("bookid") == a.bookid) {
					$(this).addClass("joinissueadd")
					$(this).text("已添加计划")
					$(this).removeClass("joinissue")
				}
			});
		});
	})
}
$(".seach_val").on("focus", function() {
	mui.back()
	$(this).blur();
})

$(document).on("tap", ".joinissue", function() {
	if($(this).hasClass("joinissueadd")) {
		return
	}
	var addbook = JSON.parse(localStorage.getItem('books')) || []
	var obj = {}
	obj.bookid = $(this).parents(".mainclass").attr("bookid")
	obj.booksrc = $(this).parents(".mainclass").find(".format img").attr("src")
	obj.bookname = $(this).parents(".mainclass").find(".title_name").text()
	addbook.push(obj)
	$(this).addClass("joinissueadd")
	$(this).text("已添加计划")
	$(this).removeClass("joinissue")
	localStorage.setItem('books', JSON.stringify(addbook))
	var list = plus.webview.getWebviewById("joinreading.html")
	mui.fire(list, 'joinreadingggg')
})